﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DMS.BaseClass;
using DMS.Data;
using DMS.Business.Modules.SystemManage;
using DMS.Business.Modules.SystemManagement;
using DMS.Business.Utility;

namespace DMS.Web.Modules.SystemManagement
{
    public partial class EnumForm : System.Web.UI.Page
    {
        private string _id = "";
        private ActionType _action;


        protected void Page_Load(object sender, EventArgs e)
        {
            _id = Request["id"];
            _action = (ActionType)Enum.Parse(typeof(ActionType), Request["action"].ToString());

            if (!IsPostBack)
            {
                InitControls();
                InitData();
            }
        }

        private void InitControls()
        {
            List<SYS_EnumTypes> list = new EnumService().GetEnumTypes();
            EnumTypeID.DataSource = list;
            EnumTypeID.DataBind();
        }

        private void InitData()
        {
            if (_id != "")
            {
                SYS_Enums obj = new EnumService().GetByID(_id);
                FormBinder.BindObjectToControls(obj, divContent);
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
            {
                return;
            }

            try
            {
                EnumService service = new EnumService();
                SYS_Enums obj;
                if (_action == ActionType.New)
                {
                    obj = new SYS_Enums();
                    obj.EnumID = Guid.NewGuid().ToString();
                }
                else
                {
                    obj = service.GetByID(_id);
                }
                FormBinder.BindControlsToObject(obj, divContent);
                obj.EnumTypeName = EnumTypeID.SelectedItem.Text;
                if (!string.IsNullOrEmpty(Sequence.Text.Trim()))
                    obj.Sequence = int.Parse(Sequence.Text.Trim());

                if (_action == ActionType.New)
                {
                    service.Add(obj);
                }
                else
                {
                    service.Update();
                }

                ClientHelper.Instance.CloseWindow("保存成功", "ok", RefreshType.OpenDialog);
            }
            catch (Exception ex)
            {
                string code = LogHelper.LogException(ex);
                string msg = string.Format("操作失败，请联系管理员。\\r\\n错误码：{0}", code);
                ClientHelper.Instance.Alert(msg);
            }
        }
    }
}